<template>
  <view class="container">
    <!-- 注册页面 -->
    <view class="card card--register" v-if="currentView === 'register'">
      <view class="card-body">
        <text class="card-title">会员注册</text>
        <form @submit.prevent="register">
          <!-- 用户名 -->
          <view class="form-group">
            <uni-easyinput
                v-model="registerForm.username"
                placeholder="请输入用户名"
                required
            />
          </view>
          <!-- 密码 -->
          <view class="form-group">
            <uni-easyinput
                type="password"
                v-model="registerForm.password"
                placeholder="请输入密码"
                required
            />
          </view>
          <!-- 确认密码 -->
          <view class="form-group">
            <uni-easyinput
                type="password"
                v-model="registerForm.confirmPassword"
                placeholder="请确认密码"
                required
            />
          </view>
          <button class="btn btn-lg" @click="register">注册</button>
        </form>
        <view class="switch-form">
          <text>已有账户?
            <!-- 跳转到登录页面 -->
            <navigator url="/pages/login/login" class="switch-text">登录</navigator>
          </text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>

import {saveOwnerInfo} from '/api/owners';  // 引入你的注册API

export default {
  data() {
    return {
      currentView: 'register', // 默认显示注册页面
      registerForm: {
        username: '',
        password: '',
        confirmPassword: ''
      }
    };
  },
  methods: {
    // 注册逻辑
    async register() {
      // 前端校验：检查密码和确认密码是否一致
      if (this.registerForm.password !== this.registerForm.confirmPassword) {
        uni.showToast({
          title: '密码与确认密码不一致',
          icon: 'none',
          duration: 2000
        });
        return;
      }

      // 校验用户名是否为空
      if (!this.registerForm.username) {
        uni.showToast({
          title: '用户名不能为空',
          icon: 'none',
          duration: 2000
        });
        return;
      }

      // 校验密码的长度（例如：密码至少6个字符）
      if (this.registerForm.password.length < 6) {
        uni.showToast({
          title: '密码长度至少6位',
          icon: 'none',
          duration: 2000
        });
        return;
      }

      // 发起注册请求
      try {
        const res = await saveOwnerInfo({
          username: this.registerForm.username,
          password: this.registerForm.password
        });

        if (res.state === 200) {
          uni.showToast({
            title: '注册成功',
            icon: 'success',
            duration: 2000
          });
          // 清空表单
          this.registerForm = {
            username: '',
            password: '',
            confirmPassword: ''
          };
          // 跳转到登录页面
          setTimeout(() => {
            uni.navigateTo({
              url: '/pages/login/login'
            });
          }, 2000); // 注册成功后2秒跳转
        } else {
          uni.showToast({
            title: res.message || '注册失败',
            icon: 'none',
            duration: 2000
          });
        }
      } catch (error) {
        uni.showToast({
          title: '注册失败，请稍后再试',
          icon: 'none',
          duration: 2000
        });
        console.error('注册失败:', error);
      }
    }
  }
};
</script>

<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.card {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.card-title {
  font-size: 24px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 15px;
}

.uni-easyinput {
  width: 100%;
  border: 1px solid #ccc;
  border-radius: 4px;
}

.btn {
  width: 100%;
  padding: 12px;
  background-color: #007aff;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 16px;
}

.switch-form {
  text-align: center;
  margin-top: 15px;
}

.switch-text {
  color: #007aff;
  cursor: pointer;
  text-decoration: underline;
}
</style>
